function _g(id) {
	return document.getElementById(id);
}
function _gn(a) {
	return document.getElementsByName(a);
}
function _unesc(a) {
	return window.decodeURIComponent ? decodeURIComponent(a) : unescape(a)
}

function arrayIndexOf(arr, obj, opt_fromIndex) {
	var fromIndex = opt_fromIndex == null ? 0 : (opt_fromIndex < 0 ? Math.max(
			0, arr.length + opt_fromIndex) : opt_fromIndex);

	for ( var i = fromIndex; i < arr.length; i++) {
		if (i in arr && arr[i] === obj)
			return i;
	}
	return -1;
}

function getElementsByClass(elementClass, context) {
	if (elementClass == null) {
		alert("element class is null.");
		return null;
	}
	if (context == null)
		context = document;

	var els = context.getElementsByTagName('input' || '*');

	if (elementClass) {
		var arrayLike = {};
		var len = 0;
		for ( var i = 0, el; el = els[i]; i++) {
			var className = el.className;
			// Check if className has a split function since SVG className does not.
			if (typeof className.split == 'function'
					&& arrayIndexOf(className.split(/\s+/), elementClass) > -1) {
				arrayLike[len++] = el;
			}

		}
		arrayLike.length = len;
		return arrayLike;
	} else {
		return els;
	}
}

function getFirstElementsByClass(elementClass, context) {
	var els = getElementsByClass(elementClass, context);
	return (els && els.length > -1) ? els[0] : null;
}

function getParentForm(node) {
	var found = false;
	while (!found) {
		if (node == null)
			return null;
		if (node.nodeName == 'FORM') {
			found = true;
			return node;
		}
		node = node.parentNode;
	}
	return null;
}

function getPageHtml(currentNode) {
	var pageHtml = {};
	var parentForm = getParentForm(currentNode);
	if (!parentForm) {
		alert("cannot find parent form.");
		return false;
	}
	var eOrderBy = getFirstElementsByClass("orderBy", parentForm);
	var eOrder = getFirstElementsByClass("order", parentForm);
	var ePageNo = getFirstElementsByClass("pageNo", parentForm);
	if (!eOrderBy || !eOrder || !ePageNo) {
		alert("cannot find orderBy or order or pageNo element.");
		return false;
	}
	pageHtml.form = parentForm;
	pageHtml.orderBy = eOrderBy;
	pageHtml.order = eOrder;
	pageHtml.pageNo = ePageNo;
	return pageHtml;
}

function jumpPage(pageNo, currentNode) {
	var pageHtml = getPageHtml(currentNode);
	if (!pageHtml)
		return false;

	pageHtml.pageNo.value = pageNo;
	pageHtml.form.submit();
}

function initQuery(currentNode) {
	var pageHtml = getPageHtml(currentNode);
	if (!pageHtml)
		return false;

	pageHtml.pageNo.value = 1;
	pageHtml.orderBy.value = "";
	pageHtml.order.value = "";
	pageHtml.form.submit();
}

function sort(orderBy, currentNode, order) {
	var pageHtml = getPageHtml(currentNode);
	if (!pageHtml)
		return false;
	if (pageHtml.orderBy.value == orderBy) {
		if(order != null){
			pageHtml.order.value = order;
		}else{
			if (pageHtml.order.value == "") {
				pageHtml.order.value = "asc";
			} else if (pageHtml.order.value == "asc") {
				pageHtml.order.value = "desc";
			} else if (pageHtml.order.value == "desc") {
				pageHtml.order.value = "";
			}
		}
	} else {
		pageHtml.orderBy.value = orderBy;
		pageHtml.order.value = "asc";
	}

	pageHtml.form.submit();
}

function getDataByGrade(x, data, codeElementName) {
	var list = data;
	if (list == null)
		list = [];
	var tem_value = "";
	for ( var i = 0; i < x && list && list.length > 0; i++) {
		if (x == 2) {
			if (_gn(codeElementName[0])[0].value == '55'
					|| _gn(codeElementName[0])[0].value == '56'
					|| _gn(codeElementName[0])[0].value == '58'
					|| _gn(codeElementName[0])[0].value == '59') {
				if (i == 0)
					tem_value = _gn(codeElementName[i])[0].value;
				else
					tem_value = _gn(codeElementName[0])[0].value + "_"
							+ _gn(codeElementName[0])[0].value;
			} else {
				tem_value = _gn(codeElementName[i])[0].value;
			}
		} else {
			tem_value = _gn(codeElementName[i])[0].value;
		}
		list = findSubList(list, tem_value);
	}
	return list;
}

function showDropDown(data, codeElementName, dropPrefix, defaultOption) {
	for ( var i = 0; i < codeElementName.length; i++)
		showx(i, data, codeElementName, dropPrefix, defaultOption);
}

function showDropDownAndCallback(data, codeElementName, dropPrefix,
		defaultOption, callback) {
	for ( var i = 0; i < codeElementName.length; i++)
		showxAndCallback(i, data, codeElementName, dropPrefix, defaultOption,
				callback);
}

function showx(x, data, codeElementName, dropPrefix, defaultOption) {
	showxAndCallback(x, data, codeElementName, dropPrefix, defaultOption, null);
}

function showxAndCallback(x, data, codeElementName, dropPrefix, defaultOption,
		callback) {
	if (dropPrefix == null)
		dropPrefix = "select";
	if (defaultOption == null)
		defaultOption = '<option value="">&#35831;&#36873;&#25321;</option>';
	var containerPrefix = dropPrefix + "Container";
	var list = getDataByGrade(x, data, codeElementName);
	//if(x==2) alert(list);
	var newSelect = '<select id="' + dropPrefix + '_' + x + '">'
			+ defaultOption;
	var temp_value = "";

	for ( var i = 0; list && i < list.length; i++) {
		if (x == 1) {
			if (_gn(codeElementName[0])[0].value == '55'
					|| _gn(codeElementName[0])[0].value == '56'
					|| _gn(codeElementName[0])[0].value == '58'
					|| _gn(codeElementName[0])[0].value == '59') {
				temp_value = _gn(codeElementName[0])[0].value + "_"
						+ _gn(codeElementName[0])[0].value;
			} else {
				temp_value = _gn(codeElementName[x])[0].value;
			}
		} else if (x == 2) {
			if (_gn(codeElementName[0])[0].value == '55'
					|| _gn(codeElementName[0])[0].value == '56'
					|| _gn(codeElementName[0])[0].value == '58'
					|| _gn(codeElementName[0])[0].value == '59') {
				temp_value = _gn(codeElementName[1])[0].value;
			} else {
				temp_value = _gn(codeElementName[x])[0].value;
			}

		} else {
			temp_value = _gn(codeElementName[x])[0].value;
		}

		newSelect += '<option value="' + list[i]["code"] + '"'
				+ (temp_value + "" == list[i]["code"] ? ' selected' : "") + '>'
				+ list[i]["name"] + '</option>';
	}
	newSelect += '</select>';
	_g(containerPrefix + "_" + x).removeChild(_g(dropPrefix + "_" + x));
	equalInnerHTML(
			newSelect,
			function(f) {
				_g(containerPrefix + "_" + x).appendChild(f);
				if (callback != null)
					callback();
				_g(dropPrefix + "_" + x).onchange = function(event) {
					var drop = (window.event && window.event.srcElement) ? window.event.srcElement
							: event.target;
					var num = parseInt(drop.id.substring(dropPrefix.length + 1));

					_gn(codeElementName[num])[0].value = drop.options[drop.selectedIndex].value;
					for ( var j = num + 1; j < codeElementName.length; j++) {
						_gn(codeElementName[j])[0].value = "";
					}
					for ( var k = num + 1; k < codeElementName.length; k++) {
						showx(k, data, codeElementName, dropPrefix,
								defaultOption);
					}

					if (_gn(codeElementName[0])[0].value == '55'
							|| _gn(codeElementName[0])[0].value == '56'
							|| _gn(codeElementName[0])[0].value == '58'
							|| _gn(codeElementName[0])[0].value == '59') {
						if (_g(dropPrefix + "_2"))
							_gn(codeElementName[1])[0].value = _g(dropPrefix
									+ "_2").options[_g(dropPrefix + "_2").selectedIndex].value;
						_gn(codeElementName[2])[0].value = '';
					}

				}
			})
}

function equalInnerHTML(html, callback) {
	var f = document.createDocumentFragment();
	var isTable = html.indexOf("<tbody") > -1;
	//var d = isTable?document.createElement("table"):document.createElement("div");
	var d = document.createElement("div");
	//if(isTable) alert(d.outerHTML);
	d.innerHTML = html;
	(function() {
		if (d.firstChild) {
			f.appendChild(d.firstChild);
			setTimeout(arguments.callee, 0)
		} else {
			if (isTable)
				callback(f.firstChild.firstChild);
			else
				callback(f.firstChild);
		}
	})()
}
function findSubList(data, value) {
	var list = [];
	if (data == null)
		return list;
	for ( var i = 0; i < data.length; i++) {
		if (data[i].code == value)
			list = data[i].list;
	}
	return list;
}

function initPage() {
	jQuery(".menu-b").hide();
	jQuery(".menu-a").click(function() {
		jQuery(".menu-b").slideUp();
		jQuery(this).next().slideDown();
	});

	jQuery("#menu-switch").click(function() {
		if (jQuery("#menu-wrapper").css("display") == "none") {
			jQuery("#yui-main .yui-b").css("margin-left", "13.3077em");
			jQuery("#menu-wrapper").show();
			jQuery("#menu-switch").removeClass("menu-switch-off");
			jQuery("#menu-switch").addClass("menu-switch-on");
		} else {
			jQuery("#menu-wrapper").hide();
			jQuery("#yui-main .yui-b").css("margin-left", "0");
			jQuery("#menu-switch").removeClass("menu-switch-on");
			jQuery("#menu-switch").addClass("menu-switch-off");
		}
	});

	jQuery(".go-top").click(function() {
		window.scrollTo(0, 0);
	});
	jQuery(".orderBy").each(function() {
		var orderBy = this.value;
		//alert(getParentForm(this).innerHTML);
		//alert(jQuery(this).parent().parent().parent().parent().parent("form").html())
		var form = jQuery(getParentForm(this));
		var direction = jQuery(".order", form).val();
		jQuery(".sorting", form).each(function() {
			var e = jQuery(this);
			var s = jQuery('<span class="span-sorting"></span>');
			e.append(s);
			if (orderBy.value != "" && e.hasClass("order-" + orderBy)) {
				if ("asc" == direction)
					s.addClass("sorting-asc");
				else if ("desc" == direction)
					s.addClass("sorting-desc");
				else
					s.addClass("sorting-default");
			} else
				s.addClass("sorting-default");
		});
	});
	if (jQuery(document).height() > jQuery("#mainBody").height() + 130)
		jQuery("#mainBody").height(jQuery(document).height() - 125);
}

function initSite() {
	jQuery(".orderBy").each(function() {
		var orderBy = this.value;
		//alert(getParentForm(this).innerHTML);
		//alert(jQuery(this).parent().parent().parent().parent().parent("form").html())
		var form = jQuery(getParentForm(this));
		var direction = jQuery(".order", form).val();
		jQuery(".sorting", form).each(function() {
			var e = jQuery(this);
			var s = jQuery('<span class="span-sorting"></span>');
			e.append(s);
			if (orderBy.value != "" && e.hasClass("order-" + orderBy)) {
				if ("asc" == direction)
					s.addClass("sorting-asc");
				else if ("desc" == direction)
					s.addClass("sorting-desc");
				else
					s.addClass("sorting-default");
			} else
				s.addClass("sorting-default");
		});
	});
}

function _type(value) {
	var s = typeof value;
	if (s == 'object') {
		if (value) {
			// We cannot use constructor == Array or instanceof Array because
			// different frames have different Array objects. In IE6, if the iframe
			// where the array was created is destroyed, the array loses its
			// prototype. Then dereferencing val.splice here throws an exception, so
			// we can't use goog.isFunction. Calling typeof directly returns 'unknown'
			// so that will work. In this case, this function will return false and
			// most array functions will still work because the array is still
			// array-like (supports length and []) even though it has lost its
			// prototype.
			// Mark Miller noticed that Object.prototype.toString
			// allows access to the unforgeable [[Class]] property.
			//  15.2.4.2 Object.prototype.toString ( )
			//  When the toString method is called, the following steps are taken:
			//      1. Get the [[Class]] property of this object.
			//      2. Compute a string value by concatenating the three strings
			//         "[object ", Result(1), and "]".
			//      3. Return Result(2).
			// and this behavior survives the destruction of the execution context.
			if (value instanceof Array
					|| // Works quickly in same execution context.
					// If value is from a different execution context then
					// !(value instanceof Object), which lets us early out in the common
					// case when value is from the same context but not an array.
					// The {if (value)} check above means we don't have to worry about
					// undefined behavior of Object.prototype.toString on null/undefined.
					//
					// HACK: In order to use an Object prototype method on the arbitrary
					//   value, the compiler requires the value be cast to type Object,
					//   even though the ECMA spec explicitly allows it.
					(!(value instanceof Object)
							&& (Object.prototype.toString.call(
							/** @type {Object} */
							(value)) == '[object Array]') ||

					// In IE all non value types are wrapped as objects across window
					// boundaries (not iframe though) so we have to do object detection
					// for this edge case
					typeof value.length == 'number'
							&& typeof value.splice != 'undefined'
							&& typeof value.propertyIsEnumerable != 'undefined'
							&& !value.propertyIsEnumerable('splice')

					)) {
				return 'array';
			}
			// HACK: There is still an array case that fails.
			//     function ArrayImpostor() {}
			//     ArrayImpostor.prototype = [];
			//     var impostor = new ArrayImpostor;
			// this can be fixed by getting rid of the fast path
			// (value instanceof Array) and solely relying on
			// (value && Object.prototype.toString.vall(value) === '[object Array]')
			// but that would require many more function calls and is not warranted
			// unless closure code is receiving objects from untrusted sources.

			// IE in cross-window calls does not correctly marshal the function type
			// (it appears just as an object) so we cannot use just typeof val ==
			// 'function'. However, if the object has a call property, it is a
			// function.
			if (!(value instanceof Object)
					&& (Object.prototype.toString.call(
					/** @type {Object} */
					(value)) == '[object Function]' || typeof value.call != 'undefined'
							&& typeof value.propertyIsEnumerable != 'undefined'
							&& !value.propertyIsEnumerable('call'))) {
				return 'function';
			}

		} else {
			return 'null';
		}

	} else if (s == 'function' && typeof value.call == 'undefined') {
		// In Safari typeof nodeList returns 'function', and on Firefox
		// typeof behaves similarly for HTML{Applet,Embed,Object}Elements
		// and RegExps.  We would like to return object for those and we can
		// detect an invalid function by making sure that the function
		// object has a call method.
		return 'object';
	}
	return s;
};

function _ajax(url, process) {
	var xhr = window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP")
			: new XMLHttpRequest();
	/*    */
	try {
		//xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	} catch (e) {
	}
	var ts = new Date();
	// try replacing _= if it is there
	var ret = url.replace(/(\?|&)_=.*?(&|$)/, "$1_=" + ts + "$2");
	// if nothing was replaced, add timestamp to the end
	url = ret + ((ret == url) ? (url.match(/\?/) ? "&" : "?") + "_=" + ts : "");
	//url = encodeURI(url);
	xhr.open("GET", url, true);
	xhr.onreadystatechange = function() {
		if (xhr.readyState == 4) {
			process(xhr);
		}
	}
	xhr.setRequestHeader("Content-Type", "text/html;charset=GBK");
	xhr.send(null);
}